package com.chf.db;

import org.apache.commons.dbcp2.BasicDataSource;

import javax.sql.DataSource;

public class DbConn {
    private static DataSource dataSource;

    private DbConn() {

    }
    //添加线程锁  使线程同步
    public static synchronized DataSource GetDBConn(){
        if(dataSource==null){
            //加载mysql驱动
            String connDriverStr = "com.mysql.cj.jdbc.Driver";
//            String connDriverStr = "oracle.jdbc.driver.OracleDriver";
            //连接字符串
            //oracle连接
//            String strURL4Oracle = "jdbc:oracle:thin:@localhost:1521:XE";
//            String userName = "scott";
//            String password = "a123456";
            //mysql数据库连接
            String connMySql8 = "jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
            String userName = "root";
            String password = "123456";

            BasicDataSource basicDataSource = new BasicDataSource();
            basicDataSource.setDriverClassName(connDriverStr);
            basicDataSource.setUrl(connMySql8);
            basicDataSource.setUsername(userName);
            basicDataSource.setPassword(password);

            dataSource = basicDataSource;
        }

        return dataSource;
    }
}
